/**
 * Container Generator
 */

const componentExists = require('../utils/componentExists');

module.exports = {
  description: 'Add a Reducer',
  prompts: [
    {
      type: 'list',
      name: 'type',
      message: 'Select the base component type:',
      default: 'Stateless Function',
      choices: () => [
        'Reduce'
      ]
    },
    {
      type: 'input',
      name: 'name',
      message: 'What should it be called?',
      default: 'Form',
      validate: value => {
        if (/.+/.test(value)) {
          return componentExists(value)
            ? 'A Redeucer with this name already exists'
            : true;
        }

        return 'The name is required';
      }
    },
    {
      type: 'confirm',
      name: 'wantActionsAndReducer',
      default: true,
      message:
        'Do you want an actions/constants/selectors/reducer tuple for this container?'
    },
    {
      type: 'confirm',
      name: 'wantRecordWithInitailState',
      default: false,
      message:
        'Do you want an Immutable Record with initial state for this reducer?'
    }
  ],
  actions: data => {
    const actions = [];
    if (data.wantActionsAndReducer) {
      if (data.wantRecordWithInitailState) {
        // Moduls
        actions.push({
          type: 'add',
          path: '../src/store/Models/{{properCase name}}/index.js',
          templateFile: './reducer/model.js.hbs',
          abortOnFail: true
        });
      }

      // Actions
      actions.push({
        type: 'add',
        path: '../src/store/actions/{{properCase name}}/index.js',
        templateFile: './reducer/actions.js.hbs',
        abortOnFail: true
      });
      actions.push({
        type: 'add',
        path: '../src/store/actions/{{properCase name}}/tests/index.test.js',
        templateFile: './reducer/actions.test.js.hbs',
        abortOnFail: true
      });

      // Constants
      actions.push({
        type: 'add',
        path: '../src/store/constants/{{properCase name}}/index.js',
        templateFile: './reducer/constants.js.hbs',
        abortOnFail: true
      });

      // Selectors
      actions.push({
        type: 'add',
        path: '../src/store/selector/{{properCase name}}/index.js',
        templateFile: './reducer/selectors.js.hbs',
        abortOnFail: true
      });
      actions.push({
        type: 'add',
        path: '../src/store/selector/{{properCase name}}/index.test.js',
        templateFile: './reducer/selectors.test.js.hbs',
        abortOnFail: true
      });

      // Reducer
      actions.push({
        type: 'add',
        path: '../src/store/reducers/{{properCase name}}Reducer/index.js',
        templateFile: './reducer/reducer.js.hbs',
        abortOnFail: true
      });
      actions.push({
        type: 'add',
        path: '../src/store/reducers/{{properCase name}}Reducer/index.test.js',
        templateFile: './reducer/reducer.test.js.hbs',
        abortOnFail: true
      });
    }

    actions.push({
      type: 'prettify',
      path: '/store/'
    });

    return actions;
  }
};
